package com.jr.controller;

import com.github.pagehelper.PageInfo;
import com.jr.pojo.ParkingSpaces;
import com.jr.service.ParkingService;
import com.jr.vo.ParkingSpacesParkingRecordsVehicles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 停车场信息控制器
 */
@RestController
@RequestMapping("/api/parking")
public class ParkingController {
    
    @Resource
    private ParkingService parkingService;
    
    /**
     * 模糊分页多表查询停车场信息
     * @param pageNum 当前页码，默认为1
     * @param code 车位编号（模糊查询）
     * @param status 车位状态（模糊查询）
     * @param description 区域描述（模糊查询）
     * @return 分页结果
     */
    @GetMapping("/info/page")
    public PageInfo<ParkingSpacesParkingRecordsVehicles> findParkingInfoByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "description", required = false) String description
    ) {
        return parkingService.findParkingInfoByPage(pageNum, code, status, description);
    }
    
    /**
     * 查询全部停车位数据（包括所有属性）
     * @return 所有停车位信息列表
     */
    @GetMapping("/spaces/all")
    public List<ParkingSpaces> findAllParkingSpaces() {
        return parkingService.findAllParkingSpaces();
    }
    
    /**
     * 查询停车位的code、type、status三个属性数据和各状态数量
     * @return 包含code、type、status数据和各状态数量的Map
     */
    @GetMapping("/spaces/basic-info")
    public Map<String, Object> getParkingSpacesBasicInfo() {
        Map<String, Object> result = new HashMap<>();
        
        // 查询全部数据
        List<ParkingSpaces> allSpaces = parkingService.findAllParkingSpaces();
        
        // 提取code、type、status三个属性
        List<Map<String, Object>> basicInfoList = allSpaces.stream().map(space -> {
            Map<String, Object> basicInfo = new HashMap<>();
            basicInfo.put("code", space.getCode());
            basicInfo.put("type", space.getType());
            basicInfo.put("status", space.getStatus());
            return basicInfo;
        }).collect(Collectors.toList());
        
        // 查询各状态数量
        Map<Integer, Integer> statusCount = parkingService.findStatusCount();
        
        // 填充结果
        result.put("basicInfo", basicInfoList);
        result.put("statusCount", statusCount);
        
        return result;
    }
}